package com.izkml.bookfilp.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class PageOverlayView extends View {
    private Bitmap overlay = null;
    private int currentPosition = -1;

    public int getCurrentPosition() {
        return currentPosition;
    }

    public void setCurrentPosition(int currentPosition) {
        this.currentPosition = currentPosition;
    }

    private int currentPositionOffsetPx = 0;

    public void setCurrentPositionOffsetPx(int currentPositionOffsetPx) {
        this.currentPositionOffsetPx = currentPositionOffsetPx;
        invalidate();
    }

    public PageOverlayView(Context context) {
        super(context);
    }

    public PageOverlayView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public PageOverlayView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public PageOverlayView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        if (overlay == null && !overlay.isRecycled()) {
            super.onDraw(canvas);
        } else {
            Bitmap bitmap = overlay;
            int count = canvas.save();
            canvas.clipRect(new RectF(getLeft(), getTop(), getWidth() - currentPositionOffsetPx, getBottom()));
            canvas.drawBitmap(bitmap, 0f, 0f, null);
            canvas.restoreToCount(count);
        }
    }

    public void setOverlay(Bitmap overlay) {

        this.overlay = overlay;
    }
}
